/**
 * Created by joy on 12/09/2017.
 */
import EthRpcHelper from '../../common/helpers/chain/EthRpcHelper'
import fs from 'fs'
import EthWalletHelper from '../../common/helpers/chain/EthWalletHelper'
import CommonUtil from '../../common/utils/CommonUtil'
import prompt from 'prompt'

export default async () => {
  const schema = {
    properties: {
      host: {
        description: 'Enter host of node',
        type: 'string',
        required: true,
        default: '127.0.0.1'
      },
      port: {
        description: 'Enter port of node',
        type: 'number',
        required: true,
        default: 8545
      },
      contractFile: {
        description: 'Enter contract file',
        type: 'string',
        required: true,
        default: 'src/sols/token.sol'
      },
      fromAddress: {
        description: 'Enter address to deploy contract',
        type: 'string',
        required: true,
        default: '0x00C8C8551d5Ba534D82fFeF87226DC0724eb226b'
      }
    }
  }

  const args = await CommonUtil.getArgsFromConsole(prompt, schema)
  const { host, port, contractFile, fromAddress } = args
  const ethRpcHelper = new EthRpcHelper(host, port)
  const ethWalletHelper = new EthWalletHelper()
  const input = fs.readFileSync(contractFile)
  const compiled = await ethWalletHelper.compileContract(input.toString())
  const result = await ethWalletHelper.deployContract(ethRpcHelper, compiled, fromAddress)
  logger.info(result)
}
